﻿/// <reference path="../Controls/BaseControl.js" />
/// <reference path="ListItem.js" />

var ____a;

var $BaseListControl = TA.Turbo.Controls.BaseListControl = function BaseListControl(clientId)
{
	if (Function.IsInherit(arguments)) return;

	this.BaseControl(clientId);

	this._elements = new Object();
}
$BaseListControl.Inherit($BaseControl);



if (____a)
{
	$BaseListControl.prototype = new $BaseControl();
	$BaseListControl.prototype.BaseControl = new $BaseControl();
}


$PropertyDefinition.Define($BaseListControl, "Items", "It", new $ListItemCollection(),
{
	Serializable: $ListItemCollection,
	GetInitValue: function GetInitValue(obj)
	{
		return obj._ItemsGetInitValue();
	},
	OnChanged: function _OnItemsChanged()
	{
		this._RecreateItems();
	}
});



// PUBLIC
// PROPS
$BaseListControl.prototype.get_ItemsContainer = function get_ItemsContainer()
{
	return this.get_Control();
}


// INTERNAL
// METHODS
$BaseListControl.prototype._ItemsGetInitValue = function _ItemsGetInitValue()
{
	var lic = new $ListItemCollection();
	this._ItemsGetInitValue_Fill(lic, this.get_Control());
	return lic;
}
$BaseListControl.prototype._ItemsGetInitValue_Fill = function _ItemsGetInitValue_Fill(collection, container)
{
}
$BaseListControl.prototype._RecreateItems = function RecreateItems()
{
	this._ClearItems();

	this._CreateItems(this.get_Items(), this.get_ItemsContainer());
}


$BaseListControl.prototype._ClearItems = function _ClearItems()
{
	this.get_ItemsContainer().PerformRecursive(this._ClearItems_CheckElement.bind(this));

	this.get_ItemsContainer().ClearChildNodes();
}
$BaseListControl.prototype._ClearItems_CheckElement = function _ClearItems_CheckElement(elem)
{
	if (elem.tagName != null)
		this._ClearItems_SaveElement(elem);
}
$BaseListControl.prototype._ClearItems_SaveElement = function _ClearItems_SaveElement(elem)
{
	if (this._elements[elem.tagName] == null)
	{
		this._elements[elem.tagName] = new Array();
	}
	this._elements[elem.tagName].push(elem);
}
$BaseListControl.prototype._GetElement = function _GetElement(tagName)
{
	tagName = tagName.toUpperCase();
	if (this._elements[tagName] != null)
	{
		if (this._elements[tagName].length > 0)
			return this._elements[tagName].pop();
	}
	return document.createElement(tagName);
}
$BaseListControl.prototype._CreateItems = function _CreateItems(items, container)
{
	for (var iItem = 0; iItem < items.length; ++iItem)
	{
		var item = items[iItem], elems;
		if (_____a)
			item = new $ListItem();

		if (item.Items.length > 0)
		{
			elems = this._CreateGroupElements(item);

			this._CreateItems(item.Items, elems[elems.length - 1]);
		}
		else
		{
			elems = this._CreateOptionElements(item);
		}

		for (var iElem = 0; iElem < elems.length; ++iElem)
			container.appendChild(elems[iElem]);
	}
}
$BaseListControl.prototype._CreateGroupElements = function _CreateGroupElements(item)
{
	if (_____a)
		item = new $ListItem();

}
$BaseListControl.prototype._CreateOptionElements = function _CreateOptionElements(item)
{
	if (_____a)
		item = new $ListItem();

}
